//
//  SSTools.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/3/31.
//

import UIKit

class SSTools: NSObject {

}

func stringToFloat(str:String) -> Float {
    let optionalString: String = str
    let myInt:Float = Float(optionalString) ?? 0
    return myInt
}

    //购物车减数据
//func reduceData(id:String) {
//    if var list : Array<Dictionary> = kUserDefaults.value(forKey: kShopingCartKey) as? Array<Dictionary<String, AnyObject>>{
//        for (index, item) in list.enumerated() {
//            if id == item["id"] as! String  {
//                list.remove(at: index)
//                kUserDefaults.setValue(list, forKey: kShopingCartKey)
//                kUserDefaults.synchronize()
//                
//                var locationFee:Float = kUserDefaults.value(forKey: kShopingCartFee) as! Float
//                let itemFee = item["totalFee"]
//                locationFee = locationFee - (itemFee as! Float)
//                kUserDefaults.setValue(locationFee, forKey: kShopingCartFee)
//                kUserDefaults.synchronize()
//                let NotifyChatMsgRecv = NSNotification.Name(rawValue:kShopingCartNotify)
//                NotificationCenter.default.post(name: NotifyChatMsgRecv, object: nil)
//                return
//            }
//        }
//    }
//}

func reduceOrder(id:String) {
    if var list : Array<Dictionary> = kUserDefaults.value(forKey: kShopingCartKey) as? Array<Dictionary<String, AnyObject>>{
        for (index, item) in list.enumerated() {
            if id == item["idkey"] as! String  {
                var orders = item["items"] as! Array<Dictionary<String,AnyObject>>
                let firtOrder = orders.first
                
                orders.remove(at: 0)
                if(orders.count == 0){//移除正个id订单
                    list.remove(at: index)
                }else{//移除单个id订单
                    let dic = [
                        "idkey":id,
                        "items":orders
                    ] as [String : AnyObject]
                    list.remove(at: index)
                    list.insert(dic, at: 0)
                }
                addSynchronizeData(list: list,orderFee: firtOrder?["totalFee"] as! Float,isAdd:false)
                return
            }
        }
    }
}

//购物车新增数据
func saveOrderData(id:String,order:Dictionary<String, AnyObject>) {
    if var list : Array<Dictionary> = kUserDefaults.value(forKey: kShopingCartKey) as? Array<Dictionary<String, AnyObject>>
    {
        var isSave = false
        for (index, item) in list.enumerated() {
            if id == item["idkey"] as! String  {//key相同
                var orders:Array<Dictionary<String, AnyObject>> = item["items"] as! Array<Dictionary<String, AnyObject>>
                orders.insert(order, at: 0)//添加到数组
                isSave = true
                let dic = [
                    "idkey":id,
                    "items":orders
                ] as [String : AnyObject]//添加到字典
                list.remove(at: index)
                list.insert(dic, at: 0)
                addSynchronizeData(list: list,orderFee: order["totalFee"] as! Float,isAdd:true)
                return
            }
        }
        if !isSave{
            var arr: Array<Any> = []
            arr.append(order)
            let dic = [
                "idkey":id,
                "items":arr
            ] as [String : AnyObject]
            list.insert(dic, at: 0)
        }
        addSynchronizeData(list: list,orderFee: order["totalFee"] as! Float,isAdd:true)
    }else{
        var list: Array<Dictionary<String, AnyObject>> = []
        var arr: Array<Dictionary<String, AnyObject>> = []
        arr.append(order)
        let dic = [
            "idkey":id,
            "items":arr
        ] as [String : AnyObject]
        list.insert(dic, at: 0)
        addSynchronizeData(list: list,orderFee: order["totalFee"] as! Float,isAdd:true)
    }
}

func addSynchronizeData(list:Array<Dictionary<String, AnyObject>>,orderFee:Float,isAdd:Bool) {
    kUserDefaults.setValue(list, forKey: kShopingCartKey)
    kUserDefaults.synchronize()
    for item in list {
        debugPrint("项目==",jhToJson(item ))
    }
    //总价格
    var resultFee:Float = 0
    var resultLocationFee:Float = 0
    if let locationFee:Float = kUserDefaults.value(forKey: kShopingCartFee) as? Float
    {
        resultLocationFee = locationFee
    }else{
        resultLocationFee = 0
    }
    if(isAdd){
        resultFee = resultLocationFee + orderFee
    }else{
        resultFee = resultLocationFee - orderFee
    }
//    let itemFee = totalFee
////    item["totalFee"]
//    locationFee = locationFee - (itemFee)
    kUserDefaults.setValue(resultFee, forKey: kShopingCartFee)
    kUserDefaults.synchronize()
    let NotifyChatMsgRecv = NSNotification.Name(rawValue:kShopingCartNotify)
    NotificationCenter.default.post(name: NotifyChatMsgRecv, object: nil)
}

func getOrderData() -> Array<Dictionary<String,AnyObject>> {
    if let list : Array<Dictionary> = kUserDefaults.value(forKey: kShopingCartKey) as? Array<Dictionary<String, AnyObject>>
    {
        for item in list {
            debugPrint("bendi项目==",jhToJson(item ))
        }
        return list
    }else{
        return []
    }
}

